package com.qiaoba.auth.controller;

import com.github.pagehelper.PageHelper;
import com.qiaoba.api.log.annotation.Log;
import com.qiaoba.api.log.enums.BusinessType;
import com.qiaoba.auth.entity.SysLoginLog;
import com.qiaoba.auth.entity.param.SysLoginLogParam;
import com.qiaoba.auth.service.SysLoginLogService;
import com.qiaoba.common.base.entity.AjaxResult;
import com.qiaoba.common.base.entity.param.TimeParam;
import com.qiaoba.common.database.entity.PageQuery;
import com.qiaoba.common.database.entity.PageResult;
import com.qiaoba.poi.excel.util.ExcelUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 登陆日志 Web层
 *
 * @author ailanyin
 * @date 2023-09-24 11:18:05
 */
@Tag(name = "3.登陆日志")
@RestController
@RequestMapping("/sys/loginLog")
@RequiredArgsConstructor
public class SysLoginLogController {

    private final SysLoginLogService sysLoginLogService;

    @Operation(summary = "获取列表")
    @GetMapping("/list")
    public PageResult list(SysLoginLogParam param, PageQuery pageQuery) {
        PageHelper.startPage(pageQuery.getPageNum(), pageQuery.getPageSize());
        List<SysLoginLog> list = sysLoginLogService.selectList(param);
        return PageResult.build(list);
    }

    @Log(title = "登陆日志", businessType = BusinessType.EXPORT)
    @Operation(summary = "导出日志")
    @PreAuthorize("hasAuthority('sys:loginLog:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysLoginLogParam param) {
        List<SysLoginLog> list = sysLoginLogService.selectList(param);
        ExcelUtil.exportExcel(list, SysLoginLog.class, "登录日志", response);
    }

    @Log(title = "登陆日志", businessType = BusinessType.DELETE)
    @Operation(summary = "删除日志")
    @PreAuthorize("hasAuthority('sys:loginLog:remove')")
    @DeleteMapping("/{loginIds}")
    public AjaxResult remove(@PathVariable List<String> loginIds) {
        return AjaxResult.toAjax(sysLoginLogService.delete(loginIds));
    }

    @Log(title = "登陆日志", businessType = BusinessType.DELETE)
    @PreAuthorize("hasAuthority('sys:loginLog:remove')")
    @DeleteMapping("/clean")
    @Operation(summary = "清空日志")
    public AjaxResult clean(TimeParam param) {
        sysLoginLogService.delLog(param);
        return AjaxResult.success();
    }
}
